﻿

$$.PageBase = function (name, params, callback) {
	var me = this;
	me.IsHome = false;	//是home框架
	me.IsPresist = false;//关闭后也一直保留不被删除
	me.IsWaitStart = true;	//以等待动画开始

	me.Node = null;
	me.Params = params || {};
	me.Callback = callback;
	//if ($.type(name) !== "string") me.Node = name;

	function emptyFunc() { }
	var empty$ = $();

	me.Wait = $$.Wait;
	me.Tips = $$.Tips;
	me.Dialog = $$.Dialog;

	me.$ = function (query) {
		if (!me.Node) return empty$;
		return $(query, me.Node);
	};
	//me.OnRelax = emptyFunc;
	me.RenderOk = function () {
	    //me.OnRelax();
		$$.Load.Close();
	    me.Wait.Close();
	    if(me.Scroller) me.Scroller.Start();
	    $$.LazyImager.Shows(me.Node);
	};
	me.Render = function () { me.RenderOk();};
	me.Hide = function () {
		if (me.Node) me.Node.style.display = "none";
	};
	me.Show = function () {
		if (me.Node) me.Node.style.display = "block";
		me.RenderOk();
	};

	me._Relax = emptyFunc;	//释放页面锁定，可以再打开页面
	me.Disposed = false;//false；1 正在销毁；true 已销毁
	me._Dispose = function () {
		if (me.Disposed === true) return;
		me.Disposed = true;
		me._Relax();
		me.Tpl = $$.Tpl.Empty;
		//me.Tips = null;
		//me.Wait = null;
		me.Scroller = null;
		if (me.Node) {
			me.Node.style.display = "none";
			$(me.Node).remove();
		}
		me.Node = null;
	};

	me._Ready = emptyFunc;
	me._Render = emptyFunc;
	me._Enter = emptyFunc;
	me._Resume = me.OnResume = emptyFunc;
	//OnClose：return false 表示不关闭
	me.Close = me.OnClose = emptyFunc;

	//initbackcall(true/other)
	//return:true/false/other
	me.Init = function (initbackcall) { return true; };
	me.InitPullScroller = function (jnode, nobot, notop) {
		function calc(dist) {
			if (dist > 400) return 200 + (dist - 400) / 4;
			if (dist > 280) return 160 + (dist - 280) / 3;
			if (dist > 180) return 120 + (dist - 180) / 2.5;
			if (dist > 100) return 80 + (dist - 100) / 2;
			if (dist > 40) return 40 + (dist - 40) / 1.5;
			return dist;
		}
		if (!jnode || jnode.length < 1) return;
		var prop = jnode.hasClass("X") ? "margin-left" : "margin-top";
		var odist = parseInt(jnode.css(prop)) || 0;
		var aniprop = {}; aniprop[prop] = odist + "px";

		if (notop !== true) jnode.on("pulltop", function (evt, dist) {
			jnode.css(prop, (calc(dist) + odist) + "px");
		})
		.on("pulltopend", function (evt, dist) {
			jnode.animate(aniprop, 500);
		});

		if (nobot !== true) jnode.on("pullbottom", function (evt, dist) {
			jnode.css(prop, (odist - calc(dist)) + "px");
		})
		.on("pullbottomend", function (evt, dist) {
			jnode.animate(aniprop, 500);
		});
	};




	me.start = function () {
		function run0() {
			me._Ready();
			me.$(".ClosePage").onClick(me.Close);
			me.Tpl = new $$.Tpl(me);
			me.Scroller = new $$.Scroller(me);
			me.Render();
			me._Enter();
			$$.LazyImager.Shows(me.Node);
		}
		function run() {
			var rt = me.Init(function (rst) {
				if (rt === true || rt === false) return;
				if (rst === true) { run0(); return; }
				$$.Load.Close();
				me.Close();
			});
			if (rt === true) run0();
			else if (rt === false) {
				$$.Load.Close();
				me.Close();
			}
		}
		if (me.IsWaitStart) $$.Load();
		if (me.Node) run();
		else $$.Page.GetHtml(name, function (html) {
			if (!html) { me._Relax(); $$.Load.Close(); console.error("Page " + name + " html fail"); return; }
			var div = document.createElement("div");
			div.innerHTML = html;
			if (!div.firstChild) { $$.Load.Close(); console.error("Page " + name + " html invalid"); me._Relax(); return; }
			me.Node = div.firstElementChild || div.firstChild;
			div = null;
			run();
		});
	};
};
